<?php
/**
 * Created by PhpStorm.
 * User: mUsr
 * Date: 18/6/30
 * Time: 下午5:40
 */


/**
 * 输出最大收益前100
 * @param $params
 * @return string
 */
function gain($params)
{
    global $server;
    $collect = "total";
    $str = '';

    if (!$params['code']) {
        return "usage:<br>http://bim.jfccc.cn:9905/gain?code=SZ_000727<br>SZ_000727 is the stock code";
    }

    $collection = $server->mongo->stock->$collect;
    $cursor = $collection->find(['_id' => $params['code']]);
    foreach ($cursor as $document) {
        $str = 'code ' . $document['_id'] . "<br>";
        $str .= 'top ' . $document['num'] . "<br>";
        foreach ($document as $key => $value) {
            if (is_object($value)) {
                $str .= "<br>$key<br>";
                foreach ($value as $k => $v) {
                     if (is_object($v)) {

                    }else{
                        $str .= "$k = $v<br>";
                    }
                }
            }
        }
    }
    return $str;
}


/**
 * 买卖点
 * @param $params
 * @return string
 */
function buy_sell($params)
{
    global $server;
    $collect = "trading_point_{$params['code']}";
    $str = '';

    if (!isset($params['code']) || !isset($params['id']) || !$params['code'] || !$params['id']) {
        return "usage:<br>http://bim.jfccc.cn:9905/buy_sell?code=SZ_000727&id=s1_l2_x0_m1_xm0
        <br>SZ_000727 is the stock code<br>S1_l2_x0_m1_xm0 is the combined condition number";
    }

    $collection = $server->mongo->stock->$collect;
    $cursor = $collection->find(['_id' => $params['id']]);
    foreach ($cursor as $document) {
        $str = $document['_id'] . "<br>";
        $str .= 'total: ' . $document['total'] . "<br>";
        $str .= 'sell_num: ' . $document['sell_num'] . "<br>";
        foreach ($document as $value) {
            if (is_object($value)) {
                $str .= "<br>";
                foreach ($value as $k => $item) {
                    $str .= "$k = $item <br>";
                }
            }
        }
    }
    return $str;
}


/**
 * 全部计算结果列表
 * @param $params
 * @return string
 */
function total($params)
{
    global $server;
    $collect = "total";
    $str = 'code_list' . "<br><br>";

    $collection = $server->mongo->stock->$collect;
    $cursor = $collection->find([]);
    foreach ($cursor as $document) {
        $str .= $document['_id'] . "<br>";
    }
    return $str;
}


